package day13;

import java.io.*;

/*本类用于测试字节输出流*/
public class TestOut1 {
    public static void main(String[] args) {
        //method();//用于测试普通字节输出流
        method2();//用于测试高效字节输出流
    }

    private static void method() {
        //0.注意要手动创建Windows中的目标文件用来查看输出的数据
        /*注意:指定的路径必须是文件路径,不能是文件夹路径,而且文件得存在*/
        OutputStream out = null;
        try{
          //1.创建字节输出流对象
          //OutputStream out2 = new FileOutputStream(new File("D:\\ready\\2.txt"));
          out = new FileOutputStream("D:\\ready\\2.txt");

          //2.使用流写出数据
          out.write(97);
          out.write(98);
          out.write(99);
          //2.2注意要强制刷新一下输出流,方式遗漏数据
          out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {/*如果要保证代码一定能执行,就通通放在finally代码块中*/
            try {
                //3.释放资源
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method2() {
        OutputStream out = null;
        try{
            //1.创建流
//            OutputStream out2 = new BufferedOutputStream(
//                    new FileOutputStream(new File("D:\\ready\\2.txt")));
            out = new BufferedOutputStream(
                    new FileOutputStream("D:\\ready\\2.txt",true));
            //2.使用流
            out.write(100);
            out.write(100);
            out.write(100);
            //2.2刷一下数据,防止遗漏
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //3.释放流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
